# -*- coding: utf-8 -*-
# @Time: 2025/5/23 15:56
# @Author: wzd
# @Email: 2146333089@qq.com
# @File: model1.py
from django.db import models


class School(models.Model):
    """学校基础信息表"""
    school_id = models.CharField(max_length=20, primary_key=True)
    """学校唯一标识ID，建议使用教育局编码或自定义编码规则"""

    name = models.CharField(max_length=100)
    """学校全称，如"北京大学"、"清华大学附属中学"等"""

    address = models.CharField(max_length=200, blank=True, null=True)
    """学校地址，精确到门牌号，可为空"""

    contact = models.CharField(max_length=20, blank=True, null=True)
    """学校联系电话，可为空"""

    email = models.EmailField(blank=True, null=True)
    """学校官方邮箱，可为空"""

    created_at = models.DateTimeField(auto_now_add=True)
    """记录创建时间，自动生成"""

    updated_at = models.DateTimeField(auto_now=True)
    """记录更新时间，自动生成"""
    class Meta:
        db_table = 'edu_school'
    def __str__(self):
        return self.name


class Admin(models.Model):
    """系统管理员表"""
    name = models.CharField(max_length=50, unique=True)
    """管理员用户名，全局唯一，用于登录"""

    password = models.CharField(max_length=128)
    """管理员密码，存储经过哈希处理的密码值"""

    email = models.EmailField(unique=True)
    """管理员邮箱，全局唯一，用于找回密码等功能"""

    phone = models.CharField(max_length=11, blank=True, null=True)
    """管理员手机号，可为空"""

    school = models.ForeignKey(School, on_delete=models.SET_NULL, blank=True, null=True)
    """关联的学校，可为空，表示超级管理员；外键删除时置空"""

    created_at = models.DateTimeField(auto_now_add=True)
    """记录创建时间，自动生成"""

    updated_at = models.DateTimeField(auto_now=True)
    """记录更新时间，自动生成"""
    class Meta:
        db_table = 'edu_admin'
    def __str__(self):
        return self.name


class Student(models.Model):
    """学生信息表"""
    student_id = models.CharField(max_length=20)
    """学生学号，全局唯一，作为主键"""

    name = models.CharField(max_length=50)
    """学生姓名"""

    sex = models.CharField(max_length=10, choices=[('male', '男'), ('female', '女')], blank=True, null=True, default=None)

    password = models.CharField(max_length=128)
    """学生登录密码，存储哈希值"""

    email = models.EmailField(unique=True)
    """学生邮箱，全局唯一，用于接收通知等"""

    grade = models.CharField(max_length=20, blank=True, null=True)
    """学生年级，如"一年级"、"高三"等，可为空"""

    major = models.CharField(max_length=50, blank=True, null=True)
    """学生专业，如"计算机科学与技术"，可为空"""

    phone = models.CharField(max_length=11, blank=True, null=True)
    """学生手机号，可为空"""

    school = models.ForeignKey(School, on_delete=models.SET_NULL, blank=True, null=True)
    """关联的学校，可为空；外键删除时置空"""

    created_at = models.DateTimeField(auto_now_add=True)
    """记录创建时间，自动生成"""

    updated_at = models.DateTimeField(auto_now=True)
    """记录更新时间，自动生成"""
    class Meta:
        db_table = 'edu_student'
        unique_together = ('student_id', 'school')  # 添加：联合唯一约束
        constraints = [
            models.UniqueConstraint(fields=['student_id', 'school'], name='unique_student_per_school')
        ]
    def __str__(self):
        return f"{self.name} ({self.student_id})"


class Teacher(models.Model):
    """教师信息表"""
    teacher_id = models.CharField(max_length=20)
    """教师编号，全局唯一，作为主键"""

    name = models.CharField(max_length=50)
    """教师姓名"""

    sex = models.CharField(max_length=10, choices=[('male', '男'), ('female', '女')], blank=True, null=True, default=None)
    """性别"""

    introduction = models.TextField(blank=True, null=True)
    """介绍"""

    password = models.CharField(max_length=128)
    """教师登录密码，存储哈希值"""

    email = models.EmailField(unique=True)
    """教师邮箱，全局唯一，用于接收通知等"""

    title = models.CharField(max_length=50, blank=True, null=True)
    """教师职称，如"教授"、"副教授"等，可为空"""

    department = models.CharField(max_length=50, blank=True, null=True)
    """教师所属院系，如"计算机系"，可为空"""

    phone = models.CharField(max_length=11, blank=True, null=True)
    """教师手机号，可为空"""

    school = models.ForeignKey(School, on_delete=models.SET_NULL, blank=True, null=True)
    """关联的学校，可为空；外键删除时置空"""

    created_at = models.DateTimeField(auto_now_add=True)
    """记录创建时间，自动生成"""

    updated_at = models.DateTimeField(auto_now=True)
    """记录更新时间，自动生成"""

    class Meta:
        db_table = 'edu_teacher'
        unique_together = ('teacher_id', 'school')  # 添加：联合唯一约束
        constraints = [
            models.UniqueConstraint(fields=['teacher_id', 'school'], name='unique_teacher_per_school')
        ]
    def __str__(self):
        return f"{self.name} ({self.teacher_id})"